//
//  UIColor-Extention.swift
//  SQPageView
//
//  Created by ToothBond on 2017/4/24.
//  Copyright © 2017年 rensq. All rights reserved.
//

import UIKit

extension UIColor {
    class func randomColor() -> UIColor{
        return UIColor(red: CGFloat(arc4random_uniform(256))/255.0, green: CGFloat(arc4random_uniform(256))/255.0, blue: CGFloat(arc4random_uniform(256))/255.0, alpha: 1.0)
    }
    
    //Swift 默认参数
    // 特性：在extention拓展构造函数 必须以便利构造函数的形式
    //1. convenience
    //2. 必须调用 self.init()原有的一个构造函数
    
    convenience init(r : CGFloat, g : CGFloat, b : CGFloat, a : CGFloat = 1.0) {
        self.init(red: r/255.0, green: g/255.0, blue : b/255.0, alpha: a )
    }
    
    convenience init?(hexString : String) { //构造函数 有可能返回nil  构造函数不能显示的声明返回值类型 -> UIColor?  init?
        
        // ## # 0x 0X
        
        guard hexString.characters.count >= 6 else {
            return nil
        }
        
        var hexTempString = hexString.uppercased()
        if hexTempString.hasPrefix("0X") || hexTempString.hasPrefix("##") {
            hexTempString = (hexTempString as NSString).substring(from: 2)
        }
        
        if hexTempString.hasPrefix("#") {
            hexTempString = (hexTempString as NSString).substring(from: 1)
        }
        
        var range = NSRange(location: 0, length: 2)
        let rHex = (hexTempString as NSString).substring(with: range)
        range.location = 2;
        let gHex = (hexTempString as NSString).substring(with: range)
        range.location = 4;
        let bHex = (hexTempString as NSString).substring(with: range)
        
        var r : UInt32 = 0
        var g : UInt32 = 0
        var b : UInt32 = 0
        Scanner(string: rHex).scanHexInt32(&r)
        Scanner(string: gHex).scanHexInt32(&g)
        Scanner(string: bHex).scanHexInt32(&b)
        
        self.init(r: CGFloat(r), g: CGFloat(g), b: CGFloat(b))
    }
    
    
    func getRGBValue() -> (CGFloat,CGFloat,CGFloat ) {
        guard let cmps = cgColor.components else {
            fatalError("not a rgb color")
        }
        
        return (cmps[0]*255,cmps[1]*255,cmps[2]*255)
    }
}
